#include <atomic>
#include <thread>
#include <vector>
#include <chrono>
#include <iostream>
using namespace std;

std::atomic<int> atomic_cnt(0);

void get_cmd()
{
    string cmd;
    while(true){
        cin >> cmd;
        if(cmd == "n"){
            atomic_cnt ++;
            atomic_cnt.notify_one();
        }
        else if(cmd == "a"){
            atomic_cnt ++;
            atomic_cnt.notify_all();
        }
        else if(cmd == "exit"){
            exit(0);
        }
        else{
            cerr << "not support cmd: " << cmd << endl;
        }
    }
}

void test_thread(int id)
{
    int count = 0;
    while(true){
        cout << id << "---" << endl;
        count = atomic_cnt.load();
        atomic_cnt.wait(count);
        // std::atomic_wait(&atomic_cnt, count); // Blocks the current thread
        cout << id << "+++" << endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
        count += id;
    }
}

int main() {
    thread t1(test_thread,1);
    thread t2(test_thread,2);
    t1.detach();
    t2.detach();

    get_cmd();
    return 0;
}

/*
按 n
触发一个线程

按 a
触发所有线程

*/